package com.distributer.product.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import com.alibaba.fastjson.JSONObject;
import com.distributer.product.mapper.DistributerProductMapper;
import com.distributer.product.model.DistributerProduct;
import com.distributer.product.service.DistributerProductService;

@Service
public class DistributerProductServiceImpl implements DistributerProductService {

	@Autowired
	private DistributerProductMapper distributerProductMapper;
	@Autowired
	private JedisPool jedisPool;
	
	@Override
	public void update(DistributerProduct distributerProduct) {
		distributerProductMapper.update(distributerProduct);
	}

	@Override
	public DistributerProduct findDistributerProductByJbProductCode(String jbProductCode) {
		return distributerProductMapper.queryDistributerProductByJbProductCode(jbProductCode);
	}

	@Override
	public void removeProductInfoFromCache(String jbProductCode) {
		Jedis jedis = jedisPool.getResource();
		try {
			jedis.del(jbProductCode);
		}finally{
			jedis.close();
		}
	}

	@Override
	public void cacheRefresh(DistributerProduct distributerProduct) {
		Jedis jedis = jedisPool.getResource();
		try {
			jedis.set(distributerProduct.getProductCode(), JSONObject.toJSONString(distributerProduct));
		}finally{
			jedis.close();
		}
	}

	@Override
	public DistributerProduct findDistributerProductByCache(String distributerProductCode) {
		Jedis jedis = jedisPool.getResource();
		try {
			String cacheSource = jedis.get(distributerProductCode);
			if (cacheSource == null || cacheSource.trim().equals("")) {
				return null;
			}
			return JSONObject.parseObject(cacheSource, DistributerProduct.class);
		}finally{
			jedis.close();
		}
	}
	
	

}
